package ljl.alg.jianzhioffer.round3.easy;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

public class _57_findContinuousSequence {

    /**
     * 暴力法永远别放弃
     * */
    class brute_force1 {
        public int[][] findContinuousSequence(int target) {
            int max = target / 2 + 1;
            List<List<Integer>> list = new ArrayList<>();
            for (int i = 1; i <= max; i++) {
                List<Integer> each = new ArrayList<>();
                int sum = 0;
                for (int j = i; j <= max; j++) {
                    sum += j;
                    each.add(j);
                    if (sum == target && each.size() > 1) {
                        list.add(each);
                        break;
                    }
                    if (sum > target)
                        break;
                }
            }
            int[][] res = new int[list.size()][];
            for (int i = 0; i < res.length; i++) {
                List<Integer> group = list.get(i);
                int[] each = new int[group.size()];
                res[i] = each;
                for (int j = 0; j < each.length; j++) {
                    each[j] = group.get(j);
                }
            }
            return res;
        }
    }

    /**
     * 我那个暴力有点过于的笨了
     * */
    class brute_force2 {
        public int[][] findContinuousSequence(int target) {
            int max = target / 2 + 1;
            List<int[]> list = new ArrayList<>();
            for (int i = 1; i <= max; i++) {
                int sum = 0;
                for (int j = i; j <= max; j++) {
                    sum += j;
                    if (sum == target && j - i > 0) {
                        int[] each = new int[j - i + 1];
                        list.add(each);
                        for (int k = i; k <= j ; k++) {
                            each[k - i] = k;
                        }
                        break;
                    }
                    if (sum > target)
                        break;
                }
            }
            return list.toArray(new int[0][]);
        }
    }

    class lufei {

        public int[][] findContinuousSequence(int target) {
            int l = 1, r = 2, s = 3;
            List<int[]> res = new ArrayList<>();
            while (l < r) {
                if (s == target) {
                    int[] each = new int[r - l + 1];
                    res.add(each);
                    for (int i = l; i <= r; i++) {
                        each[i - l] = i;
                    }
                }
                if (s >= target)
                    s -= l++;
                else
                    s += ++r;

            }
            return res.toArray(new int[0][]);
        }
    }

}
